package jcu.ict.profaid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class QuestionDbAdapter {
	public static final String KEY_ID = "question_id";
	public static final String KEY_USER = "user_id";
	public static final String KEY_DATE = "date";
	public static final String KEY_QUESTION = "desc_question";
	public static final String KEY_ID_COURSE = "course_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_ANSWER_TIME = "answer_time";
	public static final String KEY_QUESTION_TIME = "question_time";
	private static final String DATABASE_TABLE = "question";

	private Context context;

	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public QuestionDbAdapter(Context ctx) {
		this.context = ctx;
	}

	// --- open the database ---
	public QuestionDbAdapter open() throws SQLException {
		DBHelper = new DatabaseHelper(context);
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---closes the database ---
	public void close() {
		DBHelper.close();
	}

	// --- insert Question to question table ---
	public long insertQuestionList(String user_id, String date, String question, String course_id, String title, String questionTime ,String answertime) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_USER, user_id);
		initialValues.put(KEY_DATE, date);
		initialValues.put(KEY_QUESTION, question);
		initialValues.put(KEY_ID_COURSE, course_id);
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_ANSWER_TIME, answertime);
		initialValues.put(KEY_QUESTION_TIME, questionTime);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}

	// --- populate Question from database ---
	public Cursor getQuestion(String course_idx, String questionx) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_ID}, KEY_QUESTION + "=\"" + questionx +  "\"" + " AND "+ KEY_ID_COURSE + "=\"" + course_idx +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	public Cursor populateQuestion(String user_id, String course) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_TITLE,KEY_QUESTION,KEY_DATE,KEY_QUESTION_TIME,KEY_ID}, KEY_ID_COURSE + "=\"" + course +  "\"" + "AND " + KEY_USER + "=\"" + user_id +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	public Cursor populateQuestionDesc(String Id) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_QUESTION}, KEY_ID + "=\"" + Id +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	public Cursor populateQuestionEdit(String IdQuestion, String IdCourse ) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_TITLE,KEY_QUESTION}, KEY_ID + "=\"" + IdQuestion +  "\"" + "AND " + KEY_ID_COURSE + "=\"" + IdCourse +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	public int DeleteQuestion(String IdQuestion, String IdCourse ) throws SQLException {
		return db.delete(DATABASE_TABLE, KEY_ID + "=\"" + IdQuestion +  "\"", null);
	}
	
	public boolean updateTitle( String QuestionId, String CourseId, String title, String question, String time) {
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, title);
		args.put(KEY_QUESTION, question);
		args.put(KEY_DATE, time);
		return db.update(DATABASE_TABLE, args,
				KEY_ID + "=\"" + QuestionId + "\"" + " AND " + KEY_ID_COURSE + "=\"" + CourseId + "\"" , null) > 0;
	}
	
}
